/**
 * 映射 store 状态到组件的计算属性
 * @param {Object} store - Pinia store 实例
 * @param {Array|Object} keysOrMapper - 状态键名数组或映射对象
 * @returns {Object} 计算属性对象
 */
export function mapState(store, keysOrMapper) {
  return Array.isArray(keysOrMapper)
    ? keysOrMapper.reduce((computed, key) => {
        computed[key] = function () {
          return store()[key];
        };
        return computed;
      }, {})
    : Object.keys(keysOrMapper).reduce((computed, key) => {
        computed[key] = function () {
          const state = store();
          const storeKey = keysOrMapper[key];
          return state[storeKey];
        };
        return computed;
      }, {});
}

/**
 * 映射 store 操作到组件的方法
 * @param {Object} store - Pinia store 实例
 * @param {Array|Object} keysOrMapper - 操作键名数组或映射对象
 * @returns {Object} 方法对象
 */
export function mapActions(store, keysOrMapper) {
  return Array.isArray(keysOrMapper)
    ? keysOrMapper.reduce((actions, key) => {
        actions[key] = function (...args) {
          return store()[key](...args);
        };
        return actions;
      }, {})
    : Object.keys(keysOrMapper).reduce((actions, key) => {
        actions[key] = function (...args) {
          const storeKey = keysOrMapper[key];
          return store()[storeKey](...args);
        };
        return actions;
      }, {});
}
export function mapWriteableState(store, keysOrMapper) {
  return Array.isArray(keysOrMapper)
    ? keysOrMapper.reduce((computed, key) => {
        computed[key] = {
          get() {
            return store()[key];
          },
          set(value) {
            store()[key] = value;
          },
        };
        return computed;
      }, {})
    : Object.keys(keysOrMapper).reduce((computed, key) => {
        computed[key] = {
          get() {
            const state = store();
            const storeKey = keysOrMapper[key];
            return state[storeKey];
          },
          set(value) {
            const state = store();
            const storeKey = keysOrMapper[key];
            state[storeKey] = value;
          },
        };
        return computed;
      }, {});
}
